package com.yoj.gateway.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    /**
     * 添加跨域映射
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        System.out.println("========CORS configuration applied!====================");
        // registry.addMapping("/**") //映射服务器中哪些http接口运行跨域访问
        //         .allowCredentials(true)
        //         .allowedOrigins("*") // 配置哪些来源有权限跨域，* 表示所有的来源
        //         .allowedMethods("*");//配置运行跨域访问方法，* 表示所有的方法
        registry.addMapping("/**")
                .allowedOrigins("http://localhost:8080")
                .allowedMethods("*")
                .allowedHeaders("*")
                .allowCredentials(true);
    }

}
